package cn.driver.coach.controller;


import cn.driver.coach.service.CoachService;
import cn.driver.common.pojo.Coach;
import cn.driver.common.tools.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 教练控制层
 */
@RestController
@RequestMapping(value = "/coach")
@CrossOrigin
@Api(tags = "coach")

public class CoachController {

    @Autowired
    private CoachService coachService;

    @ApiOperation(value = "查询")
    @GetMapping("/getCoachList")
    public ResponseData getCoachList() {
        List<Coach> list = coachService.getCoachList();
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.ok(null, "没有找到数据!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error_600();
        }
    }

    @ApiOperation(value = "添加")
    @PostMapping("/addCoach")
    public ResponseData addCoach(@RequestBody Coach coach) {
        try {
            int num = coachService.addCoach(coach);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error();
        }
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/deleteCoach")
    public ResponseData deleteCoach(@RequestParam int id) {
        try {
            int num = coachService.delete(id);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error_600();
        }
    }

    @ApiOperation(value = "修改")
    @PutMapping("/updateCoach")
    public ResponseData updateCoach(@RequestBody Coach coach) {
        try {
            int num = coachService.update(coach);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error();
        }
    }

    @ApiOperation(value = "分页")
    @GetMapping("/pageList")
    public ResponseData pageList(int Num, int Size) {
        List<Coach> list = coachService.getList(Num, Size);
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error_600();
        }

    }

    @ApiOperation(value = "模糊查询")
    @GetMapping("/mohuList")
    public ResponseData mohuList(@RequestParam String coachName) {
        List<Coach> list = coachService.mohuList(coachName);
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.ok(null, "没有找到数据!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error_600();
        }
    }
}
